﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ptool
{
    public partial class FormUrl : Form
    {
        public FormUrl()
        {
            InitializeComponent();
        }

        private void btnUrlEncode_Click(object sender, EventArgs e)
        {
            txtResult.Text = CodeUrl(txtInput.Text, UrlCode.UrlEncode);
        }

        private void btnUrlDecode_Click(object sender, EventArgs e)
        {
            txtResult.Text = CodeUrl(txtInput.Text, UrlCode.UrlDecode);
        }

        /// <summary>
        /// 字符串编码和解码
        /// </summary>
        /// <param name="InnerXml"></param>
        /// <returns></returns>
        private string CodeUrl(string InnerXml, UrlCode urlCode)
        {
            if (String.IsNullOrEmpty(InnerXml))
            {
                return "";
            }
            else
            {
                if (urlCode == UrlCode.UrlDecode)
                { return System.Web.HttpUtility.UrlDecode(InnerXml); }
                else
                {
                    return System.Web.HttpUtility.UrlEncode(InnerXml);
                }
            }
        }

        /// <summary>
        /// 字符串编码和解码枚举
        /// </summary>
        enum UrlCode { UrlDecode, UrlEncode };
    }
}
